package com.polaris.live.popup

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.R.layout
import com.polaris.live.adapter.OnlineAudienceAdapter
import com.polaris.live.common.R.color.color_202028
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.ext.formatNumberWithK
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.PopupOnlineAudienceBinding
import com.polaris.live.im.sfs.bean.live.ext.UserBasicVo
import com.polaris.live.resp.back_resp.LiveUserOnlineResp
import com.polaris.live.translation.R.string
import com.polaris.live.utils.CommonUtils
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.avatar.FrameImageUtils

/**
 * OnlineAudiencePopup
 *
 * @author Created by 半仙 on 2024/2/27/027 17:42
 */
class OnlineAudiencePopup(
    context: Context,
    private val mHomeOwner: Long,
    private val mLiveUserOnlineResp: LiveUserOnlineResp,
    private val mLiveType: Int,
    private val mAdministrators: List<Long>? = null,
    private val mOnHeadClickListener: ((otherId: Long) -> Unit)? = null,
    private val mOnSendGiftSomeOneCallback: ((base: UserBasicVo?) -> Unit)? = null,
) : BottomPopupView(context) {

    private lateinit var mBinding: PopupOnlineAudienceBinding

    private val mAdapter by lazy { OnlineAudienceAdapter(mHomeOwner, mAdministrators) }
    override fun getImplLayoutId() = layout.popup_online_audience

    @SuppressLint("SetTextI18n")
    override fun onCreate() {
        super.onCreate()
        mBinding = PopupOnlineAudienceBinding.bind(popupImplView)
        mBinding.recycler.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(context)
        }

        mBinding.apply {
            val self = mLiveUserOnlineResp.userSelf.base
            avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(self.avatarFrame))
            itemAvatar.setImageUrl(self.avatar, self.userId)

            itemName.text = self.nickname
            itemName.setNicknameColor(self.nicknameColor, color_202028)
            country.setCountry(self.country)
            ageSex.setSexAndAge(self.sex, self.birthday)
            vipView.setVipLevel(self.vipLevel)
            if (CommonUtils.isMan(self.sex)) {
                mBinding.level1.setRichsLevel(self.richs)
                mBinding.level2.setCharmsLevel(self.charms)
            } else {
                mBinding.level1.setCharmsLevel(self.charms)
                mBinding.level2.setRichsLevel(self.richs)
            }

            tvCoin.text = mLiveUserOnlineResp.userSelf.coin.formatNumberWithK()
            when (mLiveUserOnlineResp.userSelf.rank) {
                1L -> tvItemRank.setTextColor(ColorUtils.getColor(R.color.onLineFirstPlaceTextColor))
                2L -> tvItemRank.setTextColor(ColorUtils.getColor(R.color.onLineSecondPlaceTextColor))
                3L -> tvItemRank.setTextColor(ColorUtils.getColor(R.color.onLineThirdPlaceTextColor))
                else -> {
                    tvItemRank.setTextColor(ColorUtils.getColor(R.color.onLineOtherPlaceTextColor))
                }
            }
            mLiveUserOnlineResp.userSelf.background?.let {
                Glide.with(bgImg)
                    .load(ImageUtils.imgPath(it))
                    .into(bgImg)
            }
            mBinding.llFlag.setLabel(self.label)

            if (mLiveUserOnlineResp.userSelf.rank > 50 || mLiveUserOnlineResp.userSelf.rank == 0L) {
                tvItemRank.text = "-"
            } else {
                tvItemRank.text = "${mLiveUserOnlineResp.userSelf.rank}"
            }

            tvTitle.text = if (mLiveUserOnlineResp.count >= 1) {
                "${StringUtils.getString(string.online_audience)}(${mLiveUserOnlineResp.count})"
            } else {
                "${StringUtils.getString(string.online_audience)}(0)"
            }

            mAdapter.setList(mLiveUserOnlineResp.rank)

            if (mHomeOwner == UserManager.getUserId()) {
                ivUserIdentity.setImageResource(R.drawable.ic_rank_room)
                tvUserIdentity.text = StringUtils.getString(string.homeowner_string)
                tvUserIdentity.setTextColor(ColorUtils.getColor(R.color.onLineIdentityOtherTextColor))
            } else {
                ivUserIdentity.setImageResource(R.drawable.ic_rank_indentity_audience)
                tvUserIdentity.text = StringUtils.getString(string.audience_string)
                tvUserIdentity.setTextColor(ColorUtils.getColor(R.color.onLineIdentityAudienceTextColor))
                if (mAdministrators?.isNotEmpty() == true && self.userId in mAdministrators) {
                    ivUserIdentity.setImageResource(R.drawable.ic_rank_indentity_administrator)
                    tvUserIdentity.text = StringUtils.getString(string.administrator)
                    tvUserIdentity.setTextColor(ColorUtils.getColor(R.color.onLineIdentityOtherTextColor))
                }
            }

            if (mAdapter.data.isEmpty()) {
                mBinding.recycler.visibility = View.GONE
                mBinding.llNotHava.visibility = View.VISIBLE
            }

            NavigationBarUtils.setNavigationHeightIfTransparent(mBinding.vLine) { view, height ->
                val layoutParams = view.layoutParams as ViewGroup.LayoutParams
                layoutParams.height = height
                mBinding.vLine.layoutParams = layoutParams
            }
        }

        initListener()
    }

    private fun initListener() {
        mBinding.consEnd.setOnSingleClickListener {
            if (mLiveType != LiveConst.Type.PARTY_LIVE && mLiveType != LiveConst.Type.PARTY_GAME_LIVE) {
                return@setOnSingleClickListener
            }
            mOnSendGiftSomeOneCallback?.invoke(null)
            dismiss()
        }
        mAdapter.setOnItemChildClickListener { _, view, position ->
            val item = mAdapter.getItem(position)
            when (view.id) {
                R.id.cons_end -> {
                    mOnSendGiftSomeOneCallback?.invoke(item.base)
                    dismiss()
                }

                R.id.item_avatar -> {
                    mOnHeadClickListener?.invoke(item.base.userId)
                    dismiss()
                }
            }
        }
    }
}